package com.one.ui;

import com.one.component.BackGroundPanel;
import com.one.util.CheckCodeFromMysql;
import com.one.util.ScreenUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainInterface {
    JFrame jFrame = new JFrame("小乐图书馆");
    BackGroundPanel backGroundPanel = new BackGroundPanel(new ImageIcon("images//libraryle.jpg").getImage());
    final int WIDTH = 720;
    final int HEIGHT = 480;

    JTextField uName = new JTextField(15);
    JTextField uPassword = new JTextField(15);

    JButton logIn = new JButton("登录");
    JButton signUp = new JButton("注册");

    JLabel name = new JLabel("用  户  名：");
    JLabel password = new JLabel("密        码：");

    public void init() {
        //组装视图
        jFrame.setBounds((ScreenUtils.getScreenWidth()-WIDTH) / 2,
                (ScreenUtils.getScreenHeight()-HEIGHT) / 2, WIDTH, HEIGHT);

        jFrame.setPreferredSize(new Dimension(WIDTH, HEIGHT));
        jFrame.setResizable(false);
        jFrame.setIconImage(new ImageIcon("images//logo.png").getImage());

        Box vBox = Box.createVerticalBox();

        Box userNameBox = Box.createHorizontalBox();
        userNameBox.add(Box.createHorizontalStrut(120));
        userNameBox.add(name);
        userNameBox.add(Box.createHorizontalStrut(20));

        userNameBox.add(uName);
        userNameBox.add(Box.createHorizontalStrut(140));

        Box passwordBox = Box.createHorizontalBox();
        passwordBox.add(Box.createHorizontalStrut(120));
        passwordBox.add(password);
        passwordBox.add(Box.createHorizontalStrut(20));
        passwordBox.add(uPassword);
        passwordBox.add(Box.createHorizontalStrut(140));

        Box buttonBox = Box.createHorizontalBox();
        buttonBox.add(Box.createHorizontalStrut(50));
        buttonBox.add(logIn);
        buttonBox.add(Box.createHorizontalStrut(150));
        buttonBox.add(signUp);

        vBox.add(Box.createVerticalStrut(140));
        vBox.add(userNameBox);
        vBox.add(Box.createVerticalStrut(25));
        vBox.add(passwordBox);
        vBox.add(Box.createVerticalStrut(120));
        vBox.add(buttonBox);
        vBox.add(Box.createVerticalStrut(70));

        //设置事件处理
        signUp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(jFrame, "跳转至注册页面");
                jFrame.dispose();
                jFrame.setVisible(false);
                new RegistrationPage().init();
            }
        });

        logIn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //判断登录状态
                int logInState = CheckCodeFromMysql.checkCode(uName.getText().trim(), uPassword.getText().trim());

                switch (logInState) {
                    case -1:
                        JOptionPane.showMessageDialog(jFrame, "账号不存在");
                        break;
                    case 0:
                        JOptionPane.showMessageDialog(jFrame, "账号与密码不匹配。");
                        break;
                    case 1:
                        JOptionPane.showMessageDialog(jFrame, "登录成功！");
                        new ManageInterface().init();
                        jFrame.dispose();
                        break;
                }
            }
        });

        backGroundPanel.add(vBox);
        jFrame.add(backGroundPanel);

        jFrame.pack();
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
    }
    //客户端程序入口
    public static void main(String[] args) {
        new MainInterface().init();
    }
}
